﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Net.Sockets;
using HslCommunication.BasicFramework;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace HslCommunication
{
	/// <summary>
	/// 扩展的辅助类方法
	/// </summary>
	// Token: 0x02000011 RID: 17
	public static class HslExtension
	{
		/// <inheritdoc cref="M:HslCommunication.BasicFramework.SoftBasic.ByteToHexString(System.Byte[])" />
		// Token: 0x06000097 RID: 151 RVA: 0x000045A1 File Offset: 0x000027A1
		public static string ToHexString(this byte[] InBytes)
		{
			return SoftBasic.ByteToHexString(InBytes);
		}

		/// <inheritdoc cref="M:HslCommunication.BasicFramework.SoftBasic.ByteToHexString(System.Byte[],System.Char)" />
		// Token: 0x06000098 RID: 152 RVA: 0x000045A9 File Offset: 0x000027A9
		public static string ToHexString(this byte[] InBytes, char segment)
		{
			return SoftBasic.ByteToHexString(InBytes, segment);
		}

		/// <inheritdoc cref="M:HslCommunication.BasicFramework.SoftBasic.ByteToHexString(System.Byte[],System.Char,System.Int32)" />
		// Token: 0x06000099 RID: 153 RVA: 0x000045B2 File Offset: 0x000027B2
		public static string ToHexString(this byte[] InBytes, char segment, int newLineCount)
		{
			return SoftBasic.ByteToHexString(InBytes, segment, newLineCount);
		}

		/// <inheritdoc cref="M:HslCommunication.BasicFramework.SoftBasic.HexStringToBytes(System.String)" />
		// Token: 0x0600009A RID: 154 RVA: 0x000045BC File Offset: 0x000027BC
		public static byte[] ToHexBytes(this string value)
		{
			return SoftBasic.HexStringToBytes(value);
		}

		/// <inheritdoc cref="M:HslCommunication.BasicFramework.SoftBasic.BoolOnByteIndex(System.Byte,System.Int32)" />
		// Token: 0x0600009B RID: 155 RVA: 0x000045C4 File Offset: 0x000027C4
		public static bool GetBoolOnIndex(this byte value, int offset)
		{
			return SoftBasic.BoolOnByteIndex(value, offset);
		}

		/// <inheritdoc cref="M:HslCommunication.BasicFramework.SoftBasic.BoolArrayToByte(System.Boolean[])" />
		// Token: 0x0600009C RID: 156 RVA: 0x000045CD File Offset: 0x000027CD
		public static byte[] ToByteArray(this bool[] array)
		{
			return SoftBasic.BoolArrayToByte(array);
		}

		/// <inheritdoc cref="M:HslCommunication.BasicFramework.SoftBasic.ByteToBoolArray(System.Byte[],System.Int32)" />
		// Token: 0x0600009D RID: 157 RVA: 0x000045D5 File Offset: 0x000027D5
		public static bool[] ToBoolArray(this byte[] InBytes, int length)
		{
			return SoftBasic.ByteToBoolArray(InBytes, length);
		}

		/// <inheritdoc cref="M:HslCommunication.BasicFramework.SoftBasic.ByteToBoolArray(System.Byte[])" />
		// Token: 0x0600009E RID: 158 RVA: 0x000045DE File Offset: 0x000027DE
		public static bool[] ToBoolArray(this byte[] InBytes)
		{
			return SoftBasic.ByteToBoolArray(InBytes);
		}

		/// <inheritdoc cref="M:HslCommunication.BasicFramework.SoftBasic.ArrayRemoveDouble``1(``0[],System.Int32,System.Int32)" />
		// Token: 0x0600009F RID: 159 RVA: 0x000045E6 File Offset: 0x000027E6
		public static T[] RemoveDouble<T>(this T[] value, int leftLength, int rightLength)
		{
			return SoftBasic.ArrayRemoveDouble<T>(value, leftLength, rightLength);
		}

		/// <inheritdoc cref="M:HslCommunication.BasicFramework.SoftBasic.ArrayRemoveBegin``1(``0[],System.Int32)" />
		// Token: 0x060000A0 RID: 160 RVA: 0x000045F0 File Offset: 0x000027F0
		public static T[] RemoveBegin<T>(this T[] value, int length)
		{
			return SoftBasic.ArrayRemoveBegin<T>(value, length);
		}

		/// <inheritdoc cref="M:HslCommunication.BasicFramework.SoftBasic.ArrayRemoveLast``1(``0[],System.Int32)" />
		// Token: 0x060000A1 RID: 161 RVA: 0x000045F9 File Offset: 0x000027F9
		public static T[] RemoveLast<T>(this T[] value, int length)
		{
			return SoftBasic.ArrayRemoveLast<T>(value, length);
		}

		/// <inheritdoc cref="M:HslCommunication.BasicFramework.SoftBasic.ArraySelectMiddle``1(``0[],System.Int32,System.Int32)" />
		// Token: 0x060000A2 RID: 162 RVA: 0x00004602 File Offset: 0x00002802
		public static T[] SelectMiddle<T>(this T[] value, int index, int length)
		{
			return SoftBasic.ArraySelectMiddle<T>(value, index, length);
		}

		/// <inheritdoc cref="M:HslCommunication.BasicFramework.SoftBasic.ArraySelectBegin``1(``0[],System.Int32)" />
		// Token: 0x060000A3 RID: 163 RVA: 0x0000460C File Offset: 0x0000280C
		public static T[] SelectBegin<T>(this T[] value, int length)
		{
			return SoftBasic.ArraySelectBegin<T>(value, length);
		}

		/// <inheritdoc cref="M:HslCommunication.BasicFramework.SoftBasic.ArraySelectLast``1(``0[],System.Int32)" />
		// Token: 0x060000A4 RID: 164 RVA: 0x00004615 File Offset: 0x00002815
		public static T[] SelectLast<T>(this T[] value, int length)
		{
			return SoftBasic.ArraySelectLast<T>(value, length);
		}

		/// <inheritdoc cref="M:HslCommunication.BasicFramework.SoftBasic.GetValueFromJsonObject``1(Newtonsoft.Json.Linq.JObject,System.String,``0)" />
		// Token: 0x060000A5 RID: 165 RVA: 0x0000461E File Offset: 0x0000281E
		public static T GetValueOrDefault<T>(JObject jObject, string name, T defaultValue)
		{
			return SoftBasic.GetValueFromJsonObject<T>(jObject, name, defaultValue);
		}

		/// <inheritdoc cref="M:HslCommunication.BasicFramework.SoftBasic.SpliceArray``1(``0[][])" />
		// Token: 0x060000A6 RID: 166 RVA: 0x00004628 File Offset: 0x00002828
		public static T[] SpliceArray<T>(this T[] value, params T[][] arrays)
		{
			List<T[]> list = new List<T[]>(arrays.Length + 1);
			list.Add(value);
			list.AddRange(arrays);
			return SoftBasic.SpliceArray<T>(list.ToArray());
		}

		/// <summary>
		/// 将指定的数据添加到数组的每个元素上去，使用表达式树的形式实现，将会修改原数组。不适用byte类型
		/// </summary>
		/// <typeparam name="T">数组的类型</typeparam>
		/// <param name="array">原始数据</param>
		/// <param name="value">数据值</param>
		/// <returns>返回的结果信息</returns>
		// Token: 0x060000A7 RID: 167 RVA: 0x00004660 File Offset: 0x00002860
		public static T[] IncreaseBy<T>(this T[] array, T value)
		{
			bool flag = typeof(T) == typeof(byte);
			if (flag)
			{
				ParameterExpression parameterExpression = Expression.Parameter(typeof(int), "first");
				ParameterExpression parameterExpression2 = Expression.Parameter(typeof(int), "second");
				Expression expression = Expression.Add(parameterExpression, parameterExpression2);
				Expression<Func<int, int, int>> expression2 = Expression.Lambda<Func<int, int, int>>(expression, new ParameterExpression[]
				{
					parameterExpression,
					parameterExpression2
				});
				Func<int, int, int> func = expression2.Compile();
				for (int i = 0; i < array.Length; i++)
				{
					array[i] = (T)((object)((byte)func(Convert.ToInt32(array[i]), Convert.ToInt32(value))));
				}
			}
			else
			{
				ParameterExpression parameterExpression3 = Expression.Parameter(typeof(T), "first");
				ParameterExpression parameterExpression4 = Expression.Parameter(typeof(T), "second");
				Expression expression3 = Expression.Add(parameterExpression3, parameterExpression4);
				Expression<Func<T, T, T>> expression4 = Expression.Lambda<Func<T, T, T>>(expression3, new ParameterExpression[]
				{
					parameterExpression3,
					parameterExpression4
				});
				Func<T, T, T> func2 = expression4.Compile();
				for (int j = 0; j < array.Length; j++)
				{
					array[j] = func2(array[j], value);
				}
			}
			return array;
		}

		/// <summary>
		/// 拷贝当前的实例数组，是基于引用层的浅拷贝，如果类型为值类型，那就是深度拷贝，如果类型为引用类型，就是浅拷贝
		/// </summary>
		/// <typeparam name="T">类型对象</typeparam>
		/// <param name="value">数组对象</param>
		/// <returns>拷贝的结果内容</returns>
		// Token: 0x060000A8 RID: 168 RVA: 0x000047CC File Offset: 0x000029CC
		public static T[] CopyArray<T>(this T[] value)
		{
			bool flag = value == null;
			T[] result;
			if (flag)
			{
				result = null;
			}
			else
			{
				T[] array = new T[value.Length];
				Array.Copy(value, array, value.Length);
				result = array;
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.BasicFramework.SoftBasic.ArrayFormat``1(``0[])" />
		// Token: 0x060000A9 RID: 169 RVA: 0x000047FF File Offset: 0x000029FF
		public static string ToArrayString<T>(this T[] value)
		{
			return SoftBasic.ArrayFormat<T>(value);
		}

		/// <inheritdoc cref="M:HslCommunication.BasicFramework.SoftBasic.ArrayFormat``1(``0,System.String)" />
		// Token: 0x060000AA RID: 170 RVA: 0x00004807 File Offset: 0x00002A07
		public static string ToArrayString<T>(this T[] value, string format)
		{
			return SoftBasic.ArrayFormat<T>(value, format);
		}

		/// <summary>
		/// 将字符串数组转换为实际的数据数组。例如字符串格式[1,2,3,4,5]，可以转成实际的数组对象<br />
		/// Converts a string array into an actual data array. For example, the string format [1,2,3,4,5] can be converted into an actual array object
		/// </summary>
		/// <typeparam name="T">类型对象</typeparam>
		/// <param name="value">字符串数据</param>
		/// <param name="selector">转换方法</param>
		/// <returns>实际的数组</returns>
		// Token: 0x060000AB RID: 171 RVA: 0x00004810 File Offset: 0x00002A10
		public static T[] ToStringArray<T>(this string value, Func<string, T> selector)
		{
			bool flag = value.IndexOf('[') >= 0;
			if (flag)
			{
				value = value.Replace("[", "");
			}
			bool flag2 = value.IndexOf(']') >= 0;
			if (flag2)
			{
				value = value.Replace("]", "");
			}
			string[] array = value.Split(new char[]
			{
				',',
				';'
			}, StringSplitOptions.RemoveEmptyEntries);
			return Enumerable.ToArray<T>(Enumerable.Select<string, T>(array, selector));
		}

		/// <summary>
		/// 将字符串数组转换为实际的数据数组。支持byte,sbyte,bool,short,ushort,int,uint,long,ulong,float,double，使用默认的十进制，例如字符串格式[1,2,3,4,5]，可以转成实际的数组对象<br />
		/// Converts a string array into an actual data array. Support byte, sbyte, bool, short, ushort, int, uint, long, ulong, float, double, use the default decimal, 
		/// such as the string format [1,2,3,4,5], which can be converted into an actual array Object
		/// </summary>
		/// <typeparam name="T">类型对象</typeparam>
		/// <param name="value">字符串数据</param>
		/// <returns>实际的数组</returns>
		// Token: 0x060000AC RID: 172 RVA: 0x00004890 File Offset: 0x00002A90
		public static T[] ToStringArray<T>(this string value)
		{
			Type typeFromHandle = typeof(T);
			bool flag = typeFromHandle == typeof(byte);
			T[] result;
			if (flag)
			{
				result = (T[])value.ToStringArray(new Func<string, byte>(byte.Parse));
			}
			else
			{
				bool flag2 = typeFromHandle == typeof(sbyte);
				if (flag2)
				{
					result = (T[])value.ToStringArray(new Func<string, sbyte>(sbyte.Parse));
				}
				else
				{
					bool flag3 = typeFromHandle == typeof(bool);
					if (flag3)
					{
						result = (T[])value.ToStringArray(new Func<string, bool>(bool.Parse));
					}
					else
					{
						bool flag4 = typeFromHandle == typeof(short);
						if (flag4)
						{
							result = (T[])value.ToStringArray(new Func<string, short>(short.Parse));
						}
						else
						{
							bool flag5 = typeFromHandle == typeof(ushort);
							if (flag5)
							{
								result = (T[])value.ToStringArray(new Func<string, ushort>(ushort.Parse));
							}
							else
							{
								bool flag6 = typeFromHandle == typeof(int);
								if (flag6)
								{
									result = (T[])value.ToStringArray(new Func<string, int>(int.Parse));
								}
								else
								{
									bool flag7 = typeFromHandle == typeof(uint);
									if (flag7)
									{
										result = (T[])value.ToStringArray(new Func<string, uint>(uint.Parse));
									}
									else
									{
										bool flag8 = typeFromHandle == typeof(long);
										if (flag8)
										{
											result = (T[])value.ToStringArray(new Func<string, long>(long.Parse));
										}
										else
										{
											bool flag9 = typeFromHandle == typeof(ulong);
											if (flag9)
											{
												result = (T[])value.ToStringArray(new Func<string, ulong>(ulong.Parse));
											}
											else
											{
												bool flag10 = typeFromHandle == typeof(float);
												if (flag10)
												{
													result = (T[])value.ToStringArray(new Func<string, float>(float.Parse));
												}
												else
												{
													bool flag11 = typeFromHandle == typeof(double);
													if (flag11)
													{
														result = (T[])value.ToStringArray(new Func<string, double>(double.Parse));
													}
													else
													{
														bool flag12 = typeFromHandle == typeof(DateTime);
														if (flag12)
														{
															result = (T[])value.ToStringArray(new Func<string, DateTime>(DateTime.Parse));
														}
														else
														{
															bool flag13 = typeFromHandle == typeof(string);
															if (!flag13)
															{
																throw new Exception("use ToArray<T>(Func<string,T>) method instead");
															}
															result = (T[])value.ToStringArray((string m) => m);
														}
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
			return result;
		}

		/// <summary>
		/// 启动接收数据，需要传入回调方法，传递对象<br />
		/// To start receiving data, you need to pass in a callback method and pass an object
		/// </summary>
		/// <param name="socket">socket对象</param>
		/// <param name="callback">回调方法</param>
		/// <param name="obj">数据对象</param>
		/// <returns>是否启动成功</returns>
		// Token: 0x060000AD RID: 173 RVA: 0x00004B54 File Offset: 0x00002D54
		public static OperateResult BeginReceiveResult(this Socket socket, AsyncCallback callback, object obj)
		{
			OperateResult result;
			try
			{
				socket.BeginReceive(new byte[0], 0, 0, SocketFlags.None, callback, obj);
				result = OperateResult.CreateSuccessResult();
			}
			catch (Exception ex)
			{
				if (socket != null)
				{
					socket.Close();
				}
				result = new OperateResult(ex.Message);
			}
			return result;
		}

		/// <summary>
		/// 启动接收数据，需要传入回调方法，传递对象默认为socket本身<br />
		/// To start receiving data, you need to pass in a callback method. The default object is the socket itself.
		/// </summary>
		/// <param name="socket">socket对象</param>
		/// <param name="callback">回调方法</param>
		/// <returns>是否启动成功</returns>
		// Token: 0x060000AE RID: 174 RVA: 0x00004BAC File Offset: 0x00002DAC
		public static OperateResult BeginReceiveResult(this Socket socket, AsyncCallback callback)
		{
			return socket.BeginReceiveResult(callback, socket);
		}

		/// <summary>
		/// 结束挂起的异步读取，返回读取的字节数，如果成功的情况。<br />
		/// Ends the pending asynchronous read and returns the number of bytes read, if successful.
		/// </summary>
		/// <param name="socket">socket对象</param>
		/// <param name="ar">回调方法</param>
		/// <returns>是否启动成功</returns>
		// Token: 0x060000AF RID: 175 RVA: 0x00004BC8 File Offset: 0x00002DC8
		public static OperateResult<int> EndReceiveResult(this Socket socket, IAsyncResult ar)
		{
			OperateResult<int> result;
			try
			{
				result = OperateResult.CreateSuccessResult<int>(socket.EndReceive(ar));
			}
			catch (Exception ex)
			{
				if (socket != null)
				{
					socket.Close();
				}
				result = new OperateResult<int>(ex.Message);
			}
			return result;
		}

		/// <summary>
		/// 获取当前对象的JSON格式表示的字符串。<br />
		/// Gets the string represented by the JSON format of the current object.
		/// </summary>
		/// <returns>字符串对象</returns>
		// Token: 0x060000B0 RID: 176 RVA: 0x00004C14 File Offset: 0x00002E14
		public static string ToJsonString(this object obj, Formatting formatting = 1)
		{
			return JsonConvert.SerializeObject(obj, formatting);
		}
	}
}
